package com.uuuo.rxjavaandretrofit;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.uuuo.rxjavaandretrofit.entity.User;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public <U> ObservableTransformer<U, U> schedulereTransformer() {
        ObservableTransformer<U, U> transformer = new ObservableTransformer<U, U>() {
            @Override
            public ObservableSource<U> apply(Observable<U> upstream) {
                return upstream
                        .subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
        return transformer;
    }

    public void getUserInfo(View view) {
        //@Path
        RetrofitManage.getInstance().getUserService().getUserInfo(1)
                .compose(this.<User>schedulereTransformer())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.i("111111", user.toString());
                    }
                });
    }

    public void getUserByName(View view) {
        RetrofitManage.getInstance().getUserService().getUserByName("lisi")
                .compose(this.<User>schedulereTransformer())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.i("111111", user.toString());
                    }
                });
    }

    public void getUserByPwdAndAge(View view) {
        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("password", "123456");
        hashMap.put("age", 18);

        RetrofitManage.getInstance().getUserService().getUserByPwdAndAge(hashMap)
                .compose(this.<List<User>>schedulereTransformer())
                .flatMap(new Function<List<User>, ObservableSource<User>>() {
                    @Override
                    public ObservableSource<User> apply(List<User> users) throws Exception {
                        return Observable.fromIterable(users);
                    }
                })
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.i("111111", user.toString());
                    }
                });
    }

    public void uploadImage(View view) {
        // 演示 @Multipart 和 @Part
        MediaType textType = MediaType.parse("text/plain");
        RequestBody name = RequestBody.create(textType, "lisi");

        File file = new File(Environment.getExternalStorageDirectory(), "/touxiang.jpg");
        if (!file.exists()) {
            Log.i("111111", "文件不存在");
            return;
        }
        RequestBody fileRequestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", System.currentTimeMillis() + ".jpg", fileRequestBody);
        RetrofitManage.getInstance().getUserService()
                .fileUpload(name, filePart)
                .compose(this.<User>schedulereTransformer())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.i("111111", user.toString());
                    }
                });
    }
}
